/*
 * Tencent is pleased to support the open source community by making Tinker available.
 *
 * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
 *
 * Licensed under the BSD 3-Clause License (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 *
 * https://opensource.org/licenses/BSD-3-Clause
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.yylending.las.tinker.service;

import com.tencent.tinker.lib.service.DefaultTinkerResultService;
import com.tencent.tinker.lib.service.PatchResult;
import com.tencent.tinker.lib.util.TinkerLog;
import com.tencent.tinker.lib.util.TinkerServiceInternals;
import com.yylending.las.YJKD;

import java.io.File;

/**
 * optional, you can just use DefaultTinkerResultService
 * we can restart process when we are at background or screen off
 * Created by zhangshaowen on 16/4/13.
 */
public class ResultService extends DefaultTinkerResultService {
	private static final String TAG = "Tinker.ResultService";

	@Override
	public void onPatchResult(final PatchResult result) {
		if (result == null) {
			TinkerLog.e(TAG, "ResultService received null result!!!!");
			return;
		}
		TinkerLog.i(TAG, "ResultService receive result: %s", result.toString());

		//first, we want to kill the recover process
		TinkerServiceInternals.killTinkerPatchServiceProcess(getApplicationContext());

		if (result.isSuccess) {
			YJKD.saveTinkerNeedRestart(true);
			YJKD.saveTinkerResult("success");
			TinkerLog.i(TAG, "patch success, please restart process");
		} else {
			YJKD.saveTinkerResult("fail");
			TinkerLog.i(TAG, "patch fail, please check reason");
		}
		deleteRawPatchFile(new File(result.rawPatchFilePath));
	}
}
